//
//  TaskViewController.m
//  安好
//
//  Created by lianchuang on 14-11-7.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "TaskViewController.h"
#import "TaskRemindCell.h"
#import "DrinkViewController.h"
#import "StepCountViewController.h"
#import "TakeDrugsViewController.h"
#import "UserModel.h"
#import "AllFamilyModal.h"

#define CellLineColor [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1]
#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
@interface TaskViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation TaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeNavBarView];
    [self loadData];
    [self makeTableView];
    
    ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO);
    
}
#pragma mark - 设置导航条
-(void)makeNavBarView
{
    self.navigationItem.title = @"任务提醒";
    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
-(void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 加载数据
-(void)loadData
{
    self.AllFamilyDataArray = [NSMutableArray arrayWithCapacity:0];
    self.remindDataArray = [NSMutableArray arrayWithCapacity:0];
    NSArray *titleArray = @[@"喝水提醒",@"记步提醒",@"用药提醒"];//,@"疫苗提醒"
    NSArray *detialArray = @[@"再繁忙也别忘加能量",@"享受不停前进的快乐",@"按时用药防控进行时"];//,@"接种疫苗，从娃娃抓起"
    // 图标名
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < titleArray.count; i++) {
        [imgArray addObject:[NSString stringWithFormat:@"任务_%@.png",titleArray[i]]];
    }
    /**
     *  加载数据(modal)到remindDataArray
     */
    for (NSInteger i = 0; i < titleArray.count; i++) {
        NSDictionary *dic = @{@"titleName":titleArray[i],@"detial":detialArray[i],@"imgName":imgArray[i]};
        [self.remindDataArray addObject:dic];
    }
    
   
}
#pragma mark - 创建tableView 设置代理、添加头视图（横线）
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorColor:CellLineColor];
    self.tableView.backgroundColor = [UIColor whiteColor];
    //ios7
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //ios8
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}
/**
 *  设置系统的cell之间的分割线
 *
 *  @param FatherView 这是tableViewCell
 *  @param left       距离屏幕左侧的距离
 *  @param leftS      6plus距离做侧的距离
 *  @param right      距离右侧的距离
 */
-(void)setCellLineForSuperView:(UITableViewCell*)FatherView WithLeft:(CGFloat)left or6PlusLef:(CGFloat)leftS AndRight:(CGFloat)right{
    
    if (IPhone6Plus) {
        //ios7
        if ([FatherView respondsToSelector:@selector(setSeparatorInset:)]) {
            [FatherView setSeparatorInset:UIEdgeInsetsMake(0,leftS,0,right)];
        }
        //ios8
        if ([FatherView respondsToSelector:@selector(setLayoutMargins:)]) {
            [FatherView setLayoutMargins:UIEdgeInsetsMake(0,leftS,0,right)];
        }
    }else{
        //ios7
        if ([FatherView respondsToSelector:@selector(setSeparatorInset:)]) {
            [FatherView setSeparatorInset:UIEdgeInsetsMake(0,left,0,right)];
        }
        //ios8
        if ([FatherView respondsToSelector:@selector(setLayoutMargins:)]) {
            [FatherView setLayoutMargins:UIEdgeInsetsMake(0,left,0,right)];
        }
    
    }
    

}

#pragma mark - tableView的代理方法
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.remindDataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IPhone6Plus) {
        return 78;
    }else{
        return 64;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
#pragma mark ---- 加载cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TaskRemindCell";
    
    TaskRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        if (IPhone6Plus) {
            cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]firstObject];
        }
    }
    [cell.imgView setImage:[UIImage imageNamed:[self.remindDataArray[indexPath.row] objectForKey:@"imgName"]]];
    cell.imgView.clipsToBounds = YES;
    cell.imgView.layer.cornerRadius = 3;
    cell.titleLable.text = [self.remindDataArray[indexPath.row] objectForKey:@"titleName"];
    cell.detialLable.text = [self.remindDataArray[indexPath.row] objectForKey:@"detial"];
    
    [self setCellLineForSuperView:cell WithLeft:70 or6PlusLef:85 AndRight:5];
    
    return cell;

}
#pragma mark ----选中某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            DrinkViewController *drink = [[DrinkViewController alloc] init];
            [self.navigationController pushViewController:drink animated:YES];
        }
            break;
        case 1:{
            StepCountViewController *walk;
            if (kScreenHeight == 480) {
                walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController" bundle:nil];
            }
            else if (kScreenHeight == 568){
                walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_5" bundle:nil];
            }
            else if (kScreenHeight == 667){
                walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_6" bundle:nil];
            }
            else if (kScreenHeight == 736){
                walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_6+" bundle:nil];
            }
            [self.navigationController pushViewController:walk animated:YES];
        }
            break;
        case 2:{

             //用药提醒--先判断是否登录
            [self isLogin];
           
        }
            break;
            
        default:
            break;
    }

    
}
/**
 *  登录
 *  跳转到“用药提醒”
 */
- (void)Login{
    
    TakeDrugsViewController *take = [[TakeDrugsViewController alloc] init];
   [self.navigationController pushViewController:take animated:YES];
    
}



@end
